//
//  HWWebController.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/17.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit
import WebKit
class HWWebController: HWBaseViewController {
    
    var urlString = ""
    var webView:WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
    }
    
}

extension HWWebController {
    private func initUI() -> Void {
        navigationView.titleLabel.text = "游享会员"
        navigationView.backButton.setImage(UIImage(named: "fanhui"), for: .normal)
        webView = WKWebView()
        //        exhibitionWeb.configuration.userContentController.add(self, name: "go")
        webView.navigationDelegate = self
        //        webView.uiDelegate = self
        webView.load(URLRequest(url: URL(string: urlString)!))
        webView.scrollView.showsVerticalScrollIndicator = false
        webView.scrollView.bounces = false
        view.addSubview(webView)
    }
    
    private func initFrame() -> Void {
        webView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
    }
}

extension HWWebController:WKNavigationDelegate {
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void){
        let reqURL = navigationAction.request.url?.absoluteString
        if reqURL!.hasPrefix("alipays://") || reqURL!.hasPrefix("alipay://") {
            if !UIApplication.shared.openURL(navigationAction.request.url!){
                
            }else{
                
            }
            decisionHandler(WKNavigationActionPolicy.cancel)
        }else if reqURL!.hasPrefix("weixin") {
            
            let urlScheme = navigationAction.request.url?.scheme ?? ""
            let payUrl = navigationAction.request.url!
            if urlScheme == "weixin" {
                UIApplication.shared.openURL(payUrl)
            }
            decisionHandler(WKNavigationActionPolicy.allow)
            
        }else{
            decisionHandler(WKNavigationActionPolicy.allow)
        }
        
    }
}
